package com.mfl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mfl.common.PageResult;
import com.mfl.constant.MessageConstant;
import com.mfl.context.BaseContext;
import com.mfl.exception.TestExamTopicAlreadyExistsException;
import com.mfl.mapper.TestExamMapper;
import com.mfl.mapper.TestExamTopicMapper;
import com.mfl.pojo.dto.TestExamDTO;
import com.mfl.pojo.dto.TestExamTopicDTO;
import com.mfl.pojo.dto.TestExamTopicPageQueryDTO;
import com.mfl.pojo.entity.TestExam;
import com.mfl.pojo.entity.TestExamTopic;
import com.mfl.pojo.vo.TestExamTopicPageQueryVO;
import com.mfl.pojo.vo.TestExamTopicVO;
import com.mfl.service.TestExamTopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class TestExamTopicServiceImpl implements TestExamTopicService {

    @Autowired
    private TestExamTopicMapper testExamTopicMapper;
    @Autowired
    private TestExamMapper testExamMapper;

    /**
     * 分页查询试卷题目
     *
     * @param testExamTopicPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(TestExamTopicPageQueryDTO testExamTopicPageQueryDTO) {
        PageHelper.startPage(testExamTopicPageQueryDTO.getPage(), testExamTopicPageQueryDTO.getPageSize());
        Page<TestExamTopicPageQueryVO> page = testExamTopicMapper.pageQuery(testExamTopicPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 添加试卷题目
     *
     * @param testExamTopicDTO
     */
    @Transactional
    @Override
    public void insert(TestExamTopicDTO testExamTopicDTO) {
        // 查询题目是否存在
        TestExamTopic testExamTopic = testExamTopicMapper.selectByTopicName(testExamTopicDTO.getTopicName(), testExamTopicDTO.getTestExamId());
        if (testExamTopic != null) {
            throw new TestExamTopicAlreadyExistsException(MessageConstant.TEST_EXAM_TOPIC_ALREADY_EXISTS);
        }
        TestExamTopic build = TestExamTopic.builder()
                .testExamId(testExamTopicDTO.getTestExamId())
                .topicName(testExamTopicDTO.getTopicName())
                .topicOptionA(testExamTopicDTO.getTopicOptionA())
                .topicOptionB(testExamTopicDTO.getTopicOptionB())
                .topicOptionC(testExamTopicDTO.getTopicOptionC())
                .topicOptionD(testExamTopicDTO.getTopicOptionD())
                .topicRightOption(testExamTopicDTO.getTopicRightOption())
                .topicScore(testExamTopicDTO.getTopicScore())
                .createTime(LocalDateTime.now())
                .createUser(BaseContext.getCurrentId())
                .build();
        testExamTopicMapper.insert(build);
        // 新增完题目后，修改对应试卷的修改日期为题目的新增日期
        testExamMapper.update(TestExam.builder()
                .id(testExamTopicDTO.getTestExamId())
                .updateTime(LocalDateTime.now())
                .updateUser(BaseContext.getCurrentId())
                .build());
    }

    /**
     * 批量删除试卷题目
     *
     * @param ids
     */
    @Override
    public void batchDelete(List<Integer> ids) {
        testExamTopicMapper.deleteByIds(ids);
    }

    /**
     * 根据id查询试卷题目
     *
     * @param id
     * @return
     */
    @Override
    public TestExamTopicVO getTestExamTopicById(Integer id, Integer testExamId) {
        TestExamTopic testExamTopic = testExamTopicMapper.selectById(id, testExamId);
        return TestExamTopicVO.builder()
                .id(testExamTopic.getId())
                .testExamId(testExamTopic.getTestExamId())
                .topicName(testExamTopic.getTopicName())
                .topicOptionA(testExamTopic.getTopicOptionA())
                .topicOptionB(testExamTopic.getTopicOptionB())
                .topicOptionC(testExamTopic.getTopicOptionC())
                .topicOptionD(testExamTopic.getTopicOptionD())
                .topicRightOption(testExamTopic.getTopicRightOption())
                .topicScore(testExamTopic.getTopicScore())
                .build();
    }

    /**
     * 修改试卷题目
     *
     * @param testExamTopicDTO
     */
    @Override
    public void update(TestExamTopicDTO testExamTopicDTO) {
        TestExamTopic testExamTopic = TestExamTopic.builder()
                .id(testExamTopicDTO.getId())
                .testExamId(testExamTopicDTO.getTestExamId())
                .topicName(testExamTopicDTO.getTopicName())
                .topicOptionA(testExamTopicDTO.getTopicOptionA())
                .topicOptionB(testExamTopicDTO.getTopicOptionB())
                .topicOptionC(testExamTopicDTO.getTopicOptionC())
                .topicOptionD(testExamTopicDTO.getTopicOptionD())
                .topicRightOption(testExamTopicDTO.getTopicRightOption())
                .topicScore(testExamTopicDTO.getTopicScore())
                .build();
        testExamTopicMapper.update(testExamTopic);
        // 修改完题目后，修改对应试卷的修改日期为题目的新增日期
        testExamMapper.update(TestExam.builder()
                .id(testExamTopicDTO.getTestExamId())
                .updateTime(LocalDateTime.now())
                .updateUser(BaseContext.getCurrentId())
                .build());
    }

    /**
     * 根据试卷id获取试卷的所有题目
     * @return
     */
    @Override
    public List<TestExamTopicVO> getExamTopicAll(int id) {
        return testExamTopicMapper.selectAll(id);
    }
}
